# This Python file uses the following encoding: utf-8

import time
import serial


def check_serial():
    try:
        print('创建串口操作对象')
        ser1 = serial.Serial(port='/dev/ttyUSBWCH0', timeout=0.3)
        ser2 = serial.Serial(port='/dev/ttyUSBWCH1', timeout=0.3)
        ser3 = serial.Serial(port='/dev/ttyUSBWCH2', timeout=0.3)
        ser4 = serial.Serial(port='/dev/ttyUSBWCH3', timeout=0.3)
        ser5 = serial.Serial(port='/dev/ttyUSBWCH4', timeout=0.3)
        wbuf = b'test datas on serial port 1\r\n'
        print('%s - %s' % ('串口1发送', wbuf))
        ser1.write(wbuf)
        time.sleep(1)
        rbuf = ser2.readline()
        print('%s - %s' % ('串口2接收', rbuf))
        if rbuf != wbuf:
            print('串口1和串口2的发送或接收存在问题')
        time.sleep(1)

        wbuf = b'test datas on serial port 2\r\n'
        print('%s - %s' % ('串口2发送', wbuf))
        ser2.write(wbuf)
        time.sleep(1)
        rbuf = ser1.readline()
        print('%s - %s' % ('串口1接收', rbuf))
        if rbuf != wbuf:
            print('串口1和串口2的发送或接收存在问题')
        time.sleep(1)
        print('串口1和串口2测试通过')

        wbuf = b'test datas on serial port 3\r\n'
        print('%s - %s' % ('串口3发送', wbuf))
        ser3.write(wbuf)
        time.sleep(1)
        rbuf = ser3.readline()
        print('%s - %s' % ('串口3接收', rbuf))
        if rbuf != wbuf:
            print('串口3的发送或接收存在问题')
        time.sleep(1)
        print('串口3测试通过')

        wbuf = b'test datas on serial port 4\r\n'
        print('%s - %s' % ('串口4发送', wbuf))
        ser4.write(wbuf)
        time.sleep(1)
        rbuf = ser4.readline()
        print('%s - %s' % ('串口4接收', rbuf))
        if rbuf != wbuf:
            print('串口4的发送或接收存在问题')
        time.sleep(1)
        print('串口4测试通过')

        wbuf = b'test datas on serial port 5\r\n'
        print('%s - %s' % ('串口5发送', wbuf))
        ser5.write(wbuf)
        time.sleep(1)
        rbuf = ser5.readline()
        print('%s - %s' % ('串口5接收', rbuf))
        if rbuf != wbuf:
            print('串口5的发送或接收存在问题')
        time.sleep(1)
        print('串口5测试通过')
        ser1.close()
        ser2.close()
        ser3.close()
        ser4.close()
        ser5.close()
    except:
        pass








